package pers.zh.parking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Response;
import pers.zh.parking.enums.RecordStatus;
import pers.zh.parking.po.Evaluation;
import pers.zh.parking.po.ParkingRecord;
import pers.zh.parking.po.ParkingSpace;
import pers.zh.parking.service.ILotService;
import pers.zh.parking.service.IParkRecordService;
import pers.zh.parking.service.SpaceService;

@RestController
@RequestMapping("parkRecord")
public class ParkRecordController extends BaseController {

    @Autowired
    IParkRecordService recordService;
    @Autowired
    ILotService lotService;
    @Autowired
    SpaceService spaceService;

    /**
     * 预约停车场，自动分配空余车位
     * @param record
     * @return
     */
    @PostMapping("reserve")
    public Response reserve(@RequestBody ParkingRecord record) {
        if (ObjectUtils.isEmpty(record)) {
            return Response.fail_400("参数不能为空");
        }
        if (ObjectUtils.isEmpty(record.getLotId())) {
            return Response.fail_400("停车场id不能为空");
        }
        if (ObjectUtils.isEmpty(record.getUserId())) {
            return Response.fail_400("用户id不能为空");
        }
        if (recordService.hasReservedCurLot(record.getLotId(), record.getUserId())) {
            return Response.fail_500("您已预约该停车场");
        }
        if (recordService.hasReservedOther(record.getUserId())) {
            return Response.fail_500("您已预约其他停车场，请前往预约记录查看");
        }
        if (spaceService.isFull(record.getLotId())) {
            return Response.fail_500("预约失败,车位已满");
        }
        // 随机分配空余车位
        ParkingSpace space = spaceService.getRandomFreeSpace(record.getLotId());
        record.setSpaceId(space.getSpaceId());
        record.setStatus(RecordStatus.RESERVING.getStatus());
        // 新增预约记录
        if (recordService.insertRecord(record)) {
            return Response.ok(space,"预约成功");
        }
        return Response.fail_500("预约失败");
    }

    /**
     * 取消预约
     * @param recordId
     * @return
     */
    @PostMapping("reserve/cancel/{recordId}")
    public Response cancel(@PathVariable Integer recordId) {
        if (recordService.cancelOrder(recordId)) {
            return Response.ok("取消成功");
        }
        return Response.fail_500("取消预约失败");
    }

    /**
     * 预约记录
     * @param userId
     * @return
     */
    @GetMapping("reservations/{userId}/{status}")
    public Response reservations(@PathVariable long userId, @PathVariable int status) {
        return Response.ok(recordService.getReservations(userId, status), "查询用户预约记录成功");
    }

    /**
     * 开始泊车
     * @param recordId
     * @return
     */
    @PostMapping("startPark/{recordId}")
    public Response startPark(@PathVariable Integer recordId, @RequestParam("carNo") String carNo) {

        if (recordService.park(recordId, carNo)) {
            return Response.ok("泊车成功");
        } else {
            return Response.ok("泊车失败");
        }

    }

    /**
     * 支付
     * @param recordId
     * @return
     */
    @PostMapping("pay/{recordId}")
    public Response pay(@PathVariable Integer recordId) {
        if (recordService.pay(recordId)) {
            return Response.ok("支付成功");
        } else {
            return Response.fail_500("支付失败");
        }
    }

    /**
     * 评价
     */
    @PostMapping("eval")
    public Response eval(@RequestBody Evaluation evaluation) {
        if (evaluation.getRecordId() == null || evaluation.getRecordId() == 0) {
            return Response.fail_500("记录id不能为空");
        }
        if (evaluation.getEvalLevel() == null || evaluation.getEvalLevel() == 0) {
            return Response.fail_500("评价等级不能为空");
        }
        if (recordService.eval(evaluation)) {
            return Response.ok("评价成功");
        } else {
            return Response.ok("评价失败");
        }
    }

}
